const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");

// Connection URL
const dbUrl = 'mongodb://localhost:32768';
// Database Name
const dbName = 'user';
// Database options
const dbOpts = {
    useNewUrlParser: true, //生成url解析器，防止警告
    useUnifiedTopology: true, //使用统一拓扑，防止警告
    auto_reconnect: true, //自动重连
    poolSize : 40, //连接池最大连接数
    connectTimeoutMS: 500 //连接超时时间，默认值是0，就是不超时
};
// 自定义客户端对象
let mongoClient = {};

// 数据插入
mongoClient.insertDocuments = function (collectionName, insertData = {}, callback) {
    execute(client => {
        const collection = client.db(dbName).collection(collectionName);
        collection.insertMany(insertData, (err, result) => {
            assert.strictEqual(null, err);
            // console.log(result);
            console.log("Inserted " + result.insertedCount + " documents into the collection");
            closeConnect(client, result, callback);
        });
    });
}

// 数据删除
mongoClient.deleteDocuments = function (collectionName, condition = {}, callback) {
    execute(client => {
        const collection = client.db(dbName).collection(collectionName);
        collection.deleteMany(condition, (err, result) => {
            assert.strictEqual(null, err);
            // console.log(result);
            console.log("Deleted " + result.deletedCount + " documents into the collection");
            closeConnect(client, result, callback);
        });
    });
}

// 数据修改
mongoClient.updateDocuments = function (collectionName, condition = {}, editData = {}, callback) {
    execute(client => {
        const collection = client.db(dbName).collection(collectionName);
        collection.updateMany(condition, editData, (err, result) => {
            assert.strictEqual(null, err);
            // console.log(result);
            console.log("Updated " + result.modifiedCount + " documents into the collection");
            closeConnect(client, result, callback);
        });
    });
}

// 数据查询
mongoClient.findDocuments = function (collectionName, condition = {}, sort = {}, skip = 0, limit = 0, callback) {
    execute(client => {
        const collection = client.db(dbName).collection(collectionName);
        collection.find(condition)
            .sort(sort)
            .skip(skip)
            .limit(limit)
            .toArray((err, result) => {
                assert.strictEqual(null, err);
                // console.log(result);
                console.log("Find " + result.length + " documents into the collection");
                closeConnect(client, result, callback);
            });
    });
}

function execute(callback) {
    MongoClient.connect(dbUrl, dbOpts, (err,mongoClient) => {
        assert.strictEqual(null, err);
        console.log("Connected successfully to server...");
        callback(mongoClient);
    });
}

function closeConnect(client, result, callback) {
    client.close();
    console.log("connect closed...");
    callback(result);
}

module.exports = mongoClient;